// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({
  data: {
    // 倒计时
    hours: 2,
    minutes: 0,
    seconds: 0,
    timer: null,
    listData: [],
    commendlist:[],
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameccComp: wx.canIUse('input.type.nickname'),
  },
  godetails: function () {

  },
  // 方法
  gotodetails() {
    wx.navigateTo({
      url: '../details/details',
    })
  },
  onLoad: function () {

const cachedData = wx.getStorageSync('cachedData') || [];
        this.setData({
          listData: cachedData,
          commendlist:cachedData?.slice(1,4)
        })
        console.log(this.data.commendlist,'33');
    this.startCountdown();
    
},
gotoDetails(e){
    const encodedflower = encodeURIComponent(JSON.stringify(e.currentTarget.dataset.item));
    wx.navigateTo({
      url: `/pages/details/details?payload=${encodedflower}`,
    })
},
gotobuyflower(){
    wx.navigateTo({
      url: '/pages/Buyflowers/Buyflowers',
    })
},
  startCountdown: function () {
    const that = this;
    let totalSeconds = this.data.hours * 3600 + this.data.minutes * 60 + this.data.seconds;

    this.data.timer = setInterval(() => {
      if (totalSeconds <= 0) {
        clearInterval(this.data.timer);
        this.setData({
          hours: 0,
          minutes: 0,
          seconds: 0
        });
        wx.showToast({
          title: '倒计时结束',
          icon: 'none'
        });
        return;
      }

      totalSeconds--;
      const hours = Math.floor(totalSeconds / 3600);
      const minutes = Math.floor((totalSeconds % 3600) / 60);
      const seconds = totalSeconds % 60;

      that.setData({
        hours: hours.toString().padStart(2, '0'),
        minutes: minutes.toString().padStart(2, '0'),
        seconds: seconds.toString().padStart(2, '0')
      });
    }, 1000);
  },

  onUnload: function () {
    clearInterval(this.data.timer);
  },

  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    const {
      nickName
    } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const {
      avatarUrl
    } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
})